package cn.yj.core.newStudent;

import cn.yj.api.FileUploadHandler;
import cn.yj.cgs.dba.entity.Customer;
import cn.yj.cgs.dba.entity.Gift;
import cn.yj.cgs.dba.mapper.GiftMapper;
import cn.yj.common.Enums;
import cn.yj.common.UUIdUtils;
import cn.yj.commons.StringPools;
import cn.yj.commons.utils.StringUtils;
import cn.yj.core.business.service.CustomerService;
import cn.yj.entity.Base64Entity;
import cn.yj.params.check.annotation.Require;
import cn.yj.tools.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 永健
 * @since 2021-07-24 15:40
 */
@Service
public class GiftService extends ServiceImpl<GiftMapper, Gift> implements IGiftService {

    @Resource
    private CustomerService customerService;
    private int ALL_COUNT = 100;

    @Override
    public boolean save(@Require String imgBase64, @Require String userCode) {
        if (customerService.authenticated(userCode)) {

            if (countAll(userCode) >= ALL_COUNT) {
                throw new ServiceException("同学您来迟了哦～，生活用品已领取完了呢，你可以邀请好友获取奖品哦～");
            }

            Customer customer = customerService.getOne(new QueryWrapper<Customer>().lambda().eq(Customer::getUserCode
                    , userCode));
            Gift gift = getOne(new QueryWrapper<Gift>().lambda().eq(Gift::getUserCode, userCode));

            if (StringUtils.isNull(gift)) {
                gift = new Gift();
                gift.setRealName(customer.getRealName());
                gift.setUserPhone(customer.getUserPhone());
                gift.setSchoolName(customer.getSchoolName());
                gift.setUserCode(customer.getUserCode());
                gift.setTake(Enums.Common.FALSE);
                gift.setGiftCode(UUIdUtils.getCode());
            }

            // 照片
            try {
                if (imgBase64.startsWith("http")) {
                    return true;
                }
                Base64Entity base64Entity = new Base64Entity(imgBase64, Enums.Minio.Bucket.giftstudent.name().concat(
                        "/").concat(userCode.concat(StringPools.JPG)));
                base64Entity.setContentType(Enums.Minio.ContentType.IMG.getContentType());
                String upload = FileUploadHandler.getInstant().upload(base64Entity);
                gift.setLetterPath(upload);
            } catch (Exception e) {
                e.printStackTrace();
                throw new ServiceException("图片处理异常，请重试");
            }
            return super.saveOrUpdate(gift);
        }
        throw new ServiceException("请先填写学校信息哦");
    }

    @Override
    public int countAll(@Require String userCode) {
        String customerSchoolByCode = customerService.getCustomerSchoolByCode(userCode);
        return count(new QueryWrapper<Gift>().lambda().eq(Gift::getSchoolName, customerSchoolByCode));
    }
}
